<template>
  <cube-page type="button-view" title="高毛利品种" @rightBtnClick="zfilterDrawer" :rightBtn="true">
    <div slot="content">
      <div class="scroll-list-wrap" :style="{'height': getlistHeight}">
        <cube-scroll ref="scroll" :data="tableData" :options="options" @pulling-down="onPullingDown" @pulling-up="onPullingUp">
          <div class="zcardWrap">
            <div class="zcard" v-for="(v, index) in tableData" :key="index">
              <div class="zui-flex zcardRow">
                <div class="zlabel">物料</div>
                <div class="zdes">{{v.code}}</div>
              </div>
              <div class="zui-flex zcardRow">
                <div class="zlabel">名称</div>
                <div class="zdes">{{v.name}}</div>
              </div>
              <div class="zui-flex zcardRow">
                <div class="zlabel">规格</div>
                <div class="zdes">{{v.specifications}}</div>
              </div>
              <div class="zui-flex zcardRow">
                <div class="zlabel">产地</div>
                <div class="zdes">{{v.place}}</div>
              </div>
              <div class="zui-flex zcardRow">
                <div class="zlabel">时间</div>
                <div class="zdes">{{v.scdate}}</div>
              </div>
              <div class="zui-flex zcardRow">
                <div class="zlabel">目前库存</div>
                <div class="zdes">{{v.stock}}</div>
              </div>
              <div class="zui-flex zcardRow" v-if="v.area && v.area.length" v-for="(m,n) in v.area" :key="n">
                <div class="zlabel" v-if="n==0">高毛利区域</div>
                <div class="zdes">{{m.name}} ({{m.value}})</div>
              </div>
              <div class="zui-flex zcardRow zcardBtns">
                <cube-button :primary="true" class="zbtn zuiReplayBtn" @click="zreplay(v)">回复</cube-button>
              </div>
            </div>
          </div>
        </cube-scroll>
      </div>

      <drawer :show="drawerShow" @change-show="changeDrawerShow" @on-hide="onHide" @on-show="onShow">
        <div class="zuiDrawerWrap" slot="drawer">
          <div class="zrow">
            <div class="zlabel">物料</div>
            <div class="zdes">
              <cube-input class="zinput" v-model="searchOut.code" placeholder="请输入物料"></cube-input>
            </div>
          </div>
          <div class="zrow">
            <div class="zlabel">时间</div>
            <div class="zdes">
              <div class="zdatepicker" @click="showDatePicker">
                {{searchOut.date}}
                <i class="cubeic-wrong" v-show="searchOut.date" @click.stop.prevnet="searchOut.date=''"></i>
              </div>
            </div>
          </div>
          <div class="zrow">
            <div class="zlabel">点位</div>
            <div class="zdes J_inputAppend">
              <cube-input class="zinput" type="number" v-model="searchOut.point" placeholder="请输入点位数" v-clearNoNum='{type: 1}'></cube-input>
              <div class="J_inputAppendWrap">毛利以上</div>
            </div>
          </div>
          <div class="zui-flex btn-area">
            <cube-button class="zbtn J_reset" @click="zreset">重置</cube-button>
            <cube-button class="zbtn J_sub" @click="zsubmit">提交</cube-button>
          </div>

        </div>
      </drawer>

      <vue-fab v-show="$route.name == 'Profit'" @clickMainBtn="zreplay"></vue-fab>

      <cube-view></cube-view>
    </div>
  </cube-page>
</template>

<script>
import {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} from 'vuex'

import CubePage from '@/components/cube-page'
import CubeView from '@/components/cube-view'
import Drawer from '@/components/drawer'
import {zprofitList, zhospitalList, zfavoritesAdd} from '@/util/api';

export default {
  name: 'Shortage',
  components: {
    CubeView,
    CubePage,
    Drawer
  },
  computed: {
    ...mapState(['loginUserInfo']),
  },
  data () {
    return {
      showLoading: true,
      tableData: [],
      total: 0,
      start: 1,
      pageSize: 5,
      pullDownRefreshThreshold: 60,
      pullDownRefreshStop: 40,
      options: {
        pullDownRefresh: {
          threshold: 60,
          txt: '正在刷新...'
        },
        pullUpLoad: {
          threshold: 60,
          txt: {
            more: '上拉加载更多',
            noMore: '没有更多数据了...'
          }
        },
        scrollbar: true,
        click: true,
        tap: true,
        taps: true,
      },
      getlistHeight: this.zutil.getlistHeight(44),
      searchOut: {
        code: '',
        point: '',
        date: '',
      },
      drawerShow: false,
      hospitalList: [],
      datePicker: null
    }
  },
  created () {

  },
  mounted() {
    this.fetchList();
  },
  watch: {

  },
  methods: {
    ...mapMutations(['setW_shortageData']),
    async fetchBaseData () {
      const zuiCubeLoading = this.$createToast({time: 0,txt: '正在加载中...'});
      zuiCubeLoading.show();
      let res = await zhospitalList();
      res = this.zutil.formatAxiosData(res);
      zuiCubeLoading.hide();

      if(res.code == '0000'){
        if(res.data.list && res.data.list.length){
          res.data.list.forEach(v=>{
            v.text = v.label;
            this.hospitalList.push(v);
          })
        }
        // this.hospitalList = res.data.list;
        let attr = {
          value: '',
          label: '全部',
          text: '全部'
        }
        this.hospitalList.unshift(attr);
      }else{
        this.$createToast({type: 'error', time: 2000, txt: res.message || "获取医院列表失败！"}).show();
        this.$refs.scroll.forceUpdate();
      }
      this.fetchList();
    },
    resetPage () {
      this.tableData = [];
      this.total = 0;
      this.start = 1;
    },
    onPullingDown() {
      // 下拉刷新
      this.resetPage();
      this.fetchList();
    },
    onPullingUp() {
      // 上拉加载更多
      if(this.total && this.tableData.length < this.total){
        this.fetchList();
      }else if(this.total && this.tableData.length == this.total){
        this.$refs.scroll.forceUpdate();
        return;
      }
    },
    searchData () {
      this.start = 1;
      this.fetchList();
    },
    async fetchList () {
      let tbl_param = {
        page: this.start,
        pagesize: this.pageSize
      }
      this.zutil.deepClone(tbl_param, this.searchOut);

      const zuiCubeLoading = this.$createToast({time: 0,txt: '正在加载中...'});
      zuiCubeLoading.show();
      let res = await zprofitList(tbl_param);
      res = this.zutil.formatAxiosData(res);
      zuiCubeLoading.hide();

      if(res.code == '0000'){
        if(res.data.list && res.data.list.length){
          this.tableData = this.tableData.concat(res.data.list);
          this.total = res.data.total;
          this.start++;
        }else{
          this.$refs.scroll.forceUpdate();
        }
      }else{
        this.$createToast({type: 'error', time: 2000, txt: res.message || "获取缺货信息列表失败！"}).show();
        this.$refs.scroll.forceUpdate();
      }
      // 如果微信确实无法触发点击事件，再放开这段代码
      // this.zutil.initBtnsClickEvent(this, '.zuiReplayBtn', this.zreplay);
    },
    zreplay (item) {
      // item = {"id":"510000201011056669","code":"15457","specifications":"1.8mg X 8.0ml","place":"陕西省 榆林市 定边县","date":"1973-03-05","stock":387,"ordernum":401,"pay":false,"result":"话厂","arrivaldate":"1984-08-26"};
      if(item && item.id){
        this.setW_shortageData(item);
      }else{
        this.setW_shortageData(null);
      }
      this.$router.push("/profit/feedbackp");
    },
    zfavorites (item) {
      this.$createDialog({
        type: 'confirm',
        icon: 'cubeic-warn',
        content: '是否确认添加到收藏夹？',
        onConfirm: () => {
          this.zfavoritesAdd(item);
        },
        onCancel: () => {}
      }).show()
    },
    async zfavoritesAdd (item) {
      const zuiCubeLoading = this.$createToast({time: 0,txt: '正在加载中...'});
      zuiCubeLoading.show();
      let res = await zfavoritesAdd({id: item.id});
      res = this.zutil.formatAxiosData(res);
      zuiCubeLoading.hide();
      if(res.code == '0000'){
        this.$createToast({type: 'correct', time: 2000, txt: res.message || "添加到收藏夹成功"}).show();
      }else{
        this.$createToast({type: 'error', time: 2000, txt: res.message || "添加到收藏夹失败！"}).show();
      }
    },
    zgofavorites () {
      this.$router.push("/shortage/favoritesList");
    },
    zfilterDrawer () {
      this.drawerShow = true;
    },
    drawerToggle () {
      this.drawerShow = !this.drawerShow;
    },
    onHide () {

    },
    changeDrawerShow (state) {
      // 在父页面点击mask关闭页面
      this.drawerShow = state;
    },
    onShow () {

    },
    zreset () {
      this.searchOut = {
        code: '',
        point: '',
        date: '',
      }
      if(this.datePicker)
        this.datePicker.$updateProps({value: new Date()});
    },
    zsubmit () {
      this.drawerShow = false;
      this.resetPage();
      this.searchData();
    },
    showDatePicker () {
      if (!this.datePicker) {
        this.datePicker = this.$createDatePicker({
          min: new Date(1949, 0, 1),
          max: new Date(),
          value: new Date(),
          onSelect: (date, selectedVal, selectedText) => {
            this.searchOut.date = this.moment(date).format('YYYY-MM-DD');
          }
        })
      }
      this.datePicker.show();
    }
  }
}
</script>

<style scoped lang="scss">

.fab-main-container {
  /deep/ {
    .fab-item-title {
      font-size: 14px;
      padding: 7px;
      background: #fc9153!important;
      color: #fff!important;
    }
  }
}

</style>
